<?php
 
namespace Admins\Model;

use Common\Model\Model;

class TeachModel extends Model
{
    
    public function addCategory($data) {
        if (empty($data)) {
            $this->error = '添加分类数据不能为空！';
            return false;
        }
        //终极栏目设置
        $data['create_time'] = time();
        $data = M('Teachcate')->create($data, 1);
        if ($data) {
            //添加数据
            $cateid = M('Teachcate')->add($data);
            if ($cateid) {
                return $cateid;
            } else {
                $this->error = '分类添加失败！';
                return false;
            }
        } else {
            return false;
        }
    }
    
    public function getData()
    {
        $data = M('Teachcate')->order("sort desc, catid desc")->select();
        return $data;
    }
    
    public function get_one_cat($catid)
    {
        $data = M('Teachcate')->where(array('catid' => $catid))->find();
        return $data;
    }
    
    public function adadd($data)
    {
        if (empty($data)) {
            $this->error = '没有数据！';
            return false;
        }
        if (M('Teachad')->create($data)) {
            $id = M('Teachad')->add();
            if ($id) {
                if ($data['image']) {
                    service("Attachment")->api_update('', 'advert-ds' . $id, 1);
                }
                return $id;
            }
            $this->error = '信息添加失败！';
            return false;
        } else {
            return false;
        }
    }
    
    public function deleteBatch($data){
        if(empty($data)){
            $this->error='未找到要删除的信息';
            return false;
        }
        $where['ad_id']=$data;
        if(M('Teachad')->where($where)->delete()!==false){
            service("Attachment")->api_delete('advert-ad' . $data);
            return true;
        }else{
            $this->error='广告删除失败';
            return false;
        }
    }
    
}